import time

from selenium import webdriver
from selenium.webdriver.common.by import By
#实例化浏览器对象，进入邮箱登录界面
driver = webdriver.Chrome()
driver.get('https://mail.qq.com/cgi-bin/frame_html?sid=U1IOEQ1ol7HTGkYi&r=1319bf77cb81e307dfad900636af0200&lang=zh')
time.sleep(2)
# 进入对应的iframe
iframe1 = driver.find_element(By.CSS_SELECTOR,'#QQMailSdkTool_login_loginBox_qq > iframe')
driver.switch_to.frame(iframe1)
iframe=driver.find_element(By.CSS_SELECTOR,'iframe[id="ptlogin_iframe"]')
driver.switch_to.frame(iframe)
#选择账号密码登录
driver.find_element(By.CSS_SELECTOR,'div[id="bottom_qlogin"] a').click()
time.sleep(1)
#找到账号密码控件，并进行输入
#账号
name = driver.find_element(By.CSS_SELECTOR,'div[id="uinArea"] input')
name.send_keys('账号')
time.sleep(2)
#密码
pw = driver.find_element(By.CSS_SELECTOR,'input[id="p"]')
pw.send_keys('密码')
#点击登录
login = driver.find_element(By.CSS_SELECTOR,'input[id="login_button"]')
login.click()
time.sleep(4)
#点击写信按钮
write_email = driver.find_element(By.CSS_SELECTOR,'a[id="composebtn"]')
write_email.click()
time.sleep(3)
#写收信邮箱
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR,'iframe[id="mainFrame"]'))
target = driver.find_element(By.CSS_SELECTOR,'div[id="toAreaCtrl"] input')
target.send_keys('15195770699@163.com')
time.sleep(2)
#写标题
title =driver.find_element(By.CSS_SELECTOR,'input[id="subject"][title]')
title.send_keys('蔡徐坤')
time.sleep(2)
#上传本地文件
photopaht =r'F:/code/123.jpg'
photo =driver.find_element(By.CSS_SELECTOR,'span[id="composecontainer"] input')
photo.send_keys(photopaht)
time.sleep(2)
#写内容
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, 'iframe[scrolling="auto"][tabindex="3"]'))
context = driver.find_element(By.CSS_SELECTOR,'div')
context.send_keys('你干嘛')
time.sleep(2)
#发送
driver.switch_to.parent_frame()
driver.find_element(By.CSS_SELECTOR,'div[id="toolbar"] a[name="sendbtn"]').click()
time.sleep(4)

